//
//  RequestCommonParameter.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/25.
//

#import "RequestCommonParameter.h"
#import <AdSupport/AdSupport.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <UICKeyChainStore/UICKeyChainStore.h>

@implementation RequestCommonParameter


+ (NSDictionary *)parameter {
//    [RequestCommonParameter idfv];
//    [RequestCommonParameter idfa];
//    [RequestCommonParameter appVersion];
//    [RequestCommonParameter deviceName];
//    [RequestCommonParameter deviceVersion];
    //NSString *token = [NSUserDefaults.standardUserDefaults stringForKey:@"token"];
    
    Class User = NSClassFromString(@"User");
    NSString *token = [[User performSelector:@selector(user)] performSelector:@selector(token)];
    
    return @{
        @"superior": @"ios", // 终端版本
        @"chaplain": [RequestCommonParameter appVersion], // App版本，例如：1.0.0
        @"prevost": [RequestCommonParameter deviceName], // 设备名称，例如：iphoneX
        @"names": [RequestCommonParameter idfv], // 给idfv
        @"candidates": [RequestCommonParameter deviceVersion], // 设备os版本，例如：11.2/8.0.0
        @"parish": @"sulitloanapi", // 市场 写死：sulitloanapi
        @"ways": token ?: @"", // 登录后获取的用户的sessionId
        @"odd": [RequestCommonParameter idfa], // 取idfv
        @"boyfine": @"boy", // 混淆字段
    };
}

+ (NSString *)appVersion {
    NSString *appVersion = NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"];
    return appVersion ?: @"";
}
+ (NSString *)deviceName {
    NSString *deviceName = [[UIDevice currentDevice] name];
    return deviceName ?: @"";
}
+ (NSString *)deviceVersion {
    NSString *sysVersion = [[UIDevice currentDevice] systemVersion];
    return sysVersion ?: @"";
}

// 供应商的标识符
+ (NSString *)idfv {
    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    UICKeyChainStore *keychain = [UICKeyChainStore keyChainStoreWithService:@"com.example.myapp"];
    [keychain setString:idfv forKey:@"idfv"];
    return idfv ?: @"";
}
// 广告的标识符
+ (NSString *)idfa {
    static NSString *idfa = nil; //00000000-0000-0000-0000-000000000000
    if (idfa.length > 0) {
        return idfa;
    } else {
        idfa = [NSUserDefaults.standardUserDefaults stringForKey:@"idfa"];
        if (idfa.length > 0) {
            return idfa;
        }
    }
    if (@available(iOS 14, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
                    // 获取IDFA
                    idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
                    NSLog(@"IDFA: %@", idfa);
                } else {
                    NSLog(@"User did not authorize advertising tracking.");
                }
            });
        }];
    } else {
        // iOS 13及以下版本，直接获取IDFA
        idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
        NSLog(@"IDFA: %@", idfa);
    }
    if (idfa.length > 0) {
        [NSUserDefaults.standardUserDefaults setObject:idfa forKey:@"idfa"];
    }
#ifdef DEBUG
    return idfa ?: @"506006D8-94EB-4D4F-BF91-D43D4B3B76F2";
#else
    return idfa ?: @"";
#endif
}
@end
